package com.longxi.tianjintianbao.ui.presenter.energy_read;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.greedao.controller.DataUserController;
import com.longxi.tianjintianbao.model.Build;
import com.longxi.tianjintianbao.ui.activity.energy_read.EnergyWriteListActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.energy_read.IEnergyReadListAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EnergyReadListAtPresenter extends BasePresenter<IEnergyReadListAtView> {

    String metersType = "";
    String metersCategory = "";

    String buildId = "";
    int cdBuildId = 0;
    List<Build> buildList = new ArrayList<>();

    public EnergyReadListAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        mContext.setTitle(intent.getStringExtra(AppConst.BUILD) + "抄表");
        mContext.setTvLeft(MyUtils.getString(R.string.home));
        buildId = mContext.getIntent().getStringExtra(AppConst.BUILDID);
    }

    OptionsPickerView pvBuildOptions;
    /**
     * 选择房间
     */
    public void changeBuild(String metersType, String metersCategory) {
        this.metersType = metersType;
        this.metersCategory = metersCategory;
        if(buildList.size() > 0){//判断是否已拿到数据
            showBuildDialog(buildList);
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("buildid", buildId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiRetrofit.getInstance().getBuilds(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<Build>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<Build> result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                buildList = result.getData();
                showBuildDialog(buildList);
            }
        });
    }

    /**
     * 显示楼栋选择框
     * @param builds
     */
    public void showBuildDialog(List<Build> builds){
        if(pvBuildOptions != null){
            pvBuildOptions.setPicker(builds);//添加数据
            pvBuildOptions.show();
            return;
        }
        pvBuildOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                SpannableString spannableString = new SpannableString(builds.get(options1).getBuild() + " " + "切换楼栋");
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, builds.get(options1).getBuild().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                getView().getTvSelectBuild().setText(spannableString);
                cdBuildId = builds.get(options1).getBuildid();
                if(!"".equals(metersType) && !"".equals(metersCategory)){
                    toMetersList(metersType, metersCategory);
                }
            }
        })
        .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                final TextView tvTitle = (TextView) v.findViewById(R.id.tv_title);
                tvTitle.setText("请选择需抄表楼栋");
                ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvBuildOptions.returnData();
                        pvBuildOptions.dismiss();
                    }
                });

                ivCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvBuildOptions.dismiss();
                    }
                });
            }
        })
        .isDialog(true)
        .build();

        pvBuildOptions.setPicker(builds);//添加数据
        pvBuildOptions.show();
    }

    /**
     * 到水电表列表页面
     * @param metersType 1房间 2公共
     * @param metersCategory 1水表 2电表
     */
    public void toMetersList(String metersType, String metersCategory) {
        if("1".equals(metersType)){
            if(cdBuildId == 0){
                MyUtils.showToast("请先选择楼栋");
                changeBuild(metersType, metersCategory);
                return;
            }
        }

        Intent intent = new Intent(mContext, EnergyWriteListActivity.class);
        intent.putExtra(AppConst.BUILDID, cdBuildId);
        intent.putExtra(AppConst.METERTYPE, metersType);
        intent.putExtra(AppConst.METERSCATEGORY, metersCategory);
        mContext.startActivity(intent);
    }
}
